import cv2
import aircv as ac
import matplotlib.pyplot as plt

IMAGE_SIZE = 800


# 将图片缩放成短边IMAGE_SIZE
def changeImageSize(inputImage, x, y):
    xymin = min(x, y)
    rates = xymin / IMAGE_SIZE
    outputImage = cv2.resize(inputImage, (int(y / rates), int(x / rates)))
    return outputImage


# 判断短边是否小于IMAGE_SIZE，是的话执行resize
def judgeChangeable(image):
    h, w = image.shape[:2]
    if h > IMAGE_SIZE and w > IMAGE_SIZE:
        img = changeImageSize(image, h, w)
        return img
    else:
        return image


srcIm = ac.imread('image-3.png')
objIm = ac.imread('mine1.png')
# objIm = judgeChangeable(objIm)
print(objIm.shape)
match_res = ac.find_all_template(srcIm, objIm, 0.5, 100)
# match_res = ac.find_all(srcIm, objIm, 0.5, 100)
# match_res = ac.find_all_sift(srcIm, objIm, 0.5, 100)
print(match_res)

for a in match_res:
    rect_points = a['rectangle']
    TL = rect_points[0]
    BR = rect_points[3]

    srcIm = cv2.rectangle(srcIm, TL, BR, (0, 0, 255),2)
    # img2 = srcIm[TL[1]:BR[1], TL[0]:BR[0]]
    # print(img2.shape)
    # plt.imshow(img2), plt.show()
else:
    print("Can't find objIm site")
cv2.imshow("", srcIm)
cv2.imwrite("a.png",srcIm)
cv2.waitKey(0)
